package top.infopub.security.util;


import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;


/**
 * Shiro异常信息转换成字符工具
 * @author Awoke
 * @version 2018年9月17日
 * @see ShiroExceptionUtils
 * @since
 */
public class ShiroExceptionUtils {

    /**
     * 获得异常对应的tip </br>
     * 如：IncorrectCredentialsException ，则返回：用户名或密码错误！
     * @param exceptionClassName
     * @param exceptionMsg
     * @return 
     * @see
     */
    public static String getExceptionTip(String exceptionClassName, String exceptionMsg) {
        if (exceptionClassName == null) {
            return "账号或密码错误！";
        }
        if (exceptionClassName.indexOf("IncorrectCredentialsException") != -1) {
            return "账号或密码错误！";
        }
        else if (exceptionClassName.indexOf("LockedAccountException") != -1) {
            if (StringUtils.isBlank(exceptionMsg)) {
                return "该账号不允许登录！";
            }
            else {
                return exceptionMsg;
            }
        }
        else if (exceptionClassName.indexOf("UnknownAccountException") != -1) {
            return "账号或密码错误！";
        }
        else if (exceptionClassName.indexOf("IncorrectCaptchaException") != -1) {
            return "验证码错误！";
        }
        else {
            return exceptionMsg;
        }
    }

    public static void main(String[] args) {
        try {
            throw new AuthenticationException("error");
        }
        catch (AuthenticationException ex) {
            System.out.println(ex.getClass().getSimpleName() + "=" + ex.getMessage());
        }
    }
}
